package cn.yunhe.servlet;

import cn.yunhe.pojo.UserDemo;
import cn.yunhe.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        UserDemo userdemo = new UserDemo();
        userdemo.setUserName(name);
        userdemo.setUserPwd(pwd);
        UserService userService = new UserService();
        UserDemo userLogin  =  userService.loginSearch(userdemo);
       if(userLogin != null){
           HttpSession session = request.getSession();
          session.setAttribute("userLogin",userLogin);
          response.sendRedirect("QueryLimitNewsServlet");
       }else{
           request.setAttribute("errorlogin","用户名或者密码错误");
           request.getRequestDispatcher("login.jsp").forward(request,response);
       }
    }
}
